﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Acadsoc.Middleware
{
    public class SSoHelper
    {


        public static CustomPrincipal GenerateCustomPrincipal(string name)
        {
            CustomUser user = new CustomUser()
            {
                IsAuthenticated = true,
                Name = name,
                AuthenticationType = "Custom"
            };
            CustomIdentity identity = new CustomIdentity(user);

            CustomPrincipal principal = new CustomPrincipal(identity);
            return principal;
        }

        public static UserSSoModel GetUserByToken(string authority, string token)
        {
            //api验证地址
            string requesturl = authority + "/api/User";
            int timeout = 60;
            JObject o = JObject.FromObject(new { Token = token });
            string requestdata = o.ToString();
            bool ispost = true;
            string msg;
            string result = HttpHelper.HttpPostWebRequest(requesturl, timeout, requestdata, ispost, out msg);
            if (string.IsNullOrWhiteSpace(msg))
            {
                try
                {
                    JObject oresult = JObject.Parse(result);
                    ResultBase<UserSSoModel> reponsemodel = oresult.ToObject<ResultBase<UserSSoModel>>();
                    if (reponsemodel.code == "0")
                    {

                        return reponsemodel.data;

                    }
                    else
                    {
                        return null;
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }
    }
}
